#include "mainwindow.h"
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QDebug>
#include <QSqlQueryModel>
#include <QTableView>
#include <QtXml>

void testsqlite();
void testSelect();
void testXml();
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
//    testsqlite();
//    testSelect();
//    testXml();
    return a.exec();
}


void testsqlite()
{
   QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
   db.setDatabaseName("test.db");
   if(!db.open())
   {
       qDebug()<<"open error !";
   }else{
       qDebug()<<"open success !";
   }

   QSqlQuery query;
//   query.exec("create table student(id int primary key , name vchar)");
   query.exec("insert into student values (0,'yafei0')");
   query.exec("insert into student values (1,'yafei1')");
   query.exec("insert into student values (2,'yafei2')");
}

void testSelect()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test.db");
    if(!db.open())
    {
        qDebug()<<"open error !";
    }else{
        qDebug()<<"open success !";
    }
   QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("select * from student");
    model->setHeaderData(0, Qt::Horizontal ,"id");
    model->setHeaderData(1, Qt::Horizontal ,"name");
    QTableView *view = new QTableView;
    view->setModel(model);
    view->show();
}

void testXml()
{
    QDomDocument doc;
    QFile file("my.xml");
    if(!file.open(QIODevice::ReadOnly)) return ;

    if(!doc.setContent(&file))
    {
        file.close();
        return;
    }
    QDomElement docElem = doc.documentElement();
    QDomNode n = docElem.firstChild();

    while(!n.isNull())
    {
        if(n.isElement())
        {
            QDomElement e = n.toElement();
            qDebug()<<qPrintable(e.tagName())<<qPrintable(e.attribute("id"));
            QDomNodeList list = e.childNodes();
            for(int i=0 ; i<list.count() ;i++)
            {
                QDomNode node = list.at(i);
                if(node.isElement())
                {
                    qDebug()<<"  "<<qPrintable(node.toElement().tagName()) << qPrintable(node.toElement().text());
                }
            }
        }
        n = n.nextSibling();
    }


}
